using System;
using System.ComponentModel;

namespace System.Drawing
{
	/// <summary>
	/// Structure to define RGB.
	/// </summary>
	public struct RGB
	{
		/// <summary>
		/// Gets an empty RGB structure;
		/// </summary>
		public static readonly RGB Empty = new RGB();

		#region Fields
		private int red;
		private int green;
		private int blue;
        private int alpha;

		#endregion

		#region Operators
		public static bool operator ==(RGB item1, RGB item2)
		{
			return (
				item1.Red == item2.Red 
				&& item1.Green == item2.Green 
				&& item1.Blue == item2.Blue
                && item1.Alpha==item2.Alpha
				);
		}

		public static bool operator !=(RGB item1, RGB item2)
		{
			return (
				item1.Red != item2.Red 
				|| item1.Green != item2.Green 
				|| item1.Blue != item2.Blue
                || item1.Alpha != item2.Alpha
				);
		}

		#endregion

		#region Accessors
		[Description("Red component."),]
		public int Red
		{
			get
			{
				return red;
			}
			set
			{
				red = (value>255)? 255 : ((value<0)?0 : value);
			}
		}

		[Description("Green component."),]
		public int Green
		{
			get
			{
				return green;
			}
			set
			{
				green = (value>255)? 255 : ((value<0)?0 : value);
			}
		}

		[Description("Blue component."),]
		public int Blue
		{
			get
			{
				return blue;
			}
			set
			{
				blue = (value>255)? 255 : ((value<0)?0 : value);
			}
		}

        public int Alpha
        {
            get { return alpha; }
            set { alpha = value > 255 ? 255 : value < 0 ? 0 : value; }
        }

		#endregion

        public RGB(int R, int G, int B)
        {
            alpha = 255;
            red = (R > 255) ? 255 : ((R < 0) ? 0 : R);
            green = (G > 255) ? 255 : ((G < 0) ? 0 : G);
            blue = (B > 255) ? 255 : ((B < 0) ? 0 : B);
        } 

		public RGB(int A, int R, int G, int B) 
		{
            alpha = A > 255 ? 255 : A < 0 ? 0 : A;
			red = (R>255)? 255 : ((R<0)?0 : R);
			green = (G>255)? 255 : ((G<0)?0 : G);
			blue = (B>255)? 255 : ((B<0)?0 : B);
        }

        public RGB(Color color)
        {
            red = color.R;
            green = color.G;
            blue = color.B;
            alpha = color.A;
        }

        #region Methods

        public Color ToColor()
        {
            return Color.FromArgb(red, green, blue);
        }

        #endregion

        #region override Methods

        public override bool Equals(Object obj) 
		{
			if(obj==null || GetType()!=obj.GetType()) return false;

			return (this == (RGB)obj);
		}

		public override int GetHashCode() 
		{
            return Red.GetHashCode() ^ Green.GetHashCode() ^ Blue.GetHashCode() ^ Alpha.GetHashCode();
		}

		#endregion
    } 
}
